﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using ComicIsland.App.Views;
using ComicIsland.Common;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ComicIsland.App.Controls
{
    public partial class AppHelpControl : PhoneApplicationPage
    {
        private Canvas OneCanvas, TwoCanvas, ThreeCanvas, FourCanvas;

        private DoubleAnimation animation1, animation2, animation3, animation4;

        private int pageIndex;
        private bool canvasClipped;
        private ComicIslandPage _BasePageName;
        private Storyboard pageTurnStoryboard;
        private PhoneApplicationPage _BasePage;

        public AppHelpControl()
        {
            InitializeComponent();
            //
            base.ManipulationStarted += AppHelpControl_ManipulationStarted;
            base.ManipulationDelta += AppHelpControl_ManipulationDelta;
            base.ManipulationCompleted += AppHelpControl_ManipulationCompleted;
            this.pageTurnStoryboard = base.Resources["sbAppHelpControl"] as Storyboard;
        }

        public void SetPhoneApplicationPage(PhoneApplicationPage basePage, ComicIslandPage pageName)
        {
            this.pageIndex = 0;
            this._BasePageName = pageName;
            this._BasePage = basePage;
            base.Visibility = Visibility.Visible;
            this.RemapCanvasPointers();
        }

        public void RemapCanvasPointers()
        {
            int num = (int)((this.LayoutRoot.ActualWidth == 0 ? 480 : this.LayoutRoot.ActualWidth));
            this.OneCanvas = this.LayoutRoot.Children[0] as Canvas;
            this.TwoCanvas = this.LayoutRoot.Children[1] as Canvas;
            this.ThreeCanvas = this.LayoutRoot.Children[2] as Canvas;
            this.FourCanvas = this.LayoutRoot.Children[3] as Canvas;
            ImageBrush imageBrush1 = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Images/Help/1.jpg", UriKind.Relative))
            };
            this.OneCanvas.Background = imageBrush1;
            //
            ImageBrush imageBrush2 = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Images/Help/2.jpg", UriKind.Relative))
            };
            this.TwoCanvas.Background = imageBrush2;
            //
            ImageBrush imageBrush3 = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Images/Help/3.jpg", UriKind.Relative))
            };
            this.ThreeCanvas.Background = imageBrush3;
            //
            ImageBrush imageBrush4 = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri("/Images/Help/4.jpg", UriKind.Relative))
            };
            this.FourCanvas.Background = imageBrush4;
            //
            ((TranslateTransform)this.OneCanvas.RenderTransform).X = 0;
            ((TranslateTransform)this.TwoCanvas.RenderTransform).X = num;
            ((TranslateTransform)this.ThreeCanvas.RenderTransform).X = num * 2;
            ((TranslateTransform)this.FourCanvas.RenderTransform).X = num * 3;
            this.OneCanvas.Visibility = Visibility.Visible;
            this.TwoCanvas.Visibility = Visibility.Visible;
            this.ThreeCanvas.Visibility = Visibility.Visible;
            this.FourCanvas.Visibility = Visibility.Visible;
            this.animation1 = this.pageTurnStoryboard.Children[0] as DoubleAnimation;
            this.animation2 = this.pageTurnStoryboard.Children[1] as DoubleAnimation;
            this.animation3 = this.pageTurnStoryboard.Children[2] as DoubleAnimation;
            this.animation4 = this.pageTurnStoryboard.Children[3] as DoubleAnimation;
            if (!this.canvasClipped)
            {
                RectangleGeometry rectangleGeometry1 = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.OneCanvas.Clip = rectangleGeometry1;
                RectangleGeometry rectangleGeometry2 = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.TwoCanvas.Clip = rectangleGeometry2;
                RectangleGeometry rectangleGeometry3 = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.ThreeCanvas.Clip = rectangleGeometry3;
                RectangleGeometry rectangleGeometry4 = new RectangleGeometry()
                {
                    Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight)
                };
                this.FourCanvas.Clip = rectangleGeometry4;
                this.canvasClipped = true;
            }
        }

        void AppHelpControl_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            e.Handled = true;
            object originalSource = e.OriginalSource;
            e.ManipulationContainer.TransformToVisual(this).Transform(e.ManipulationOrigin);
        }

        void AppHelpControl_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
        {
            e.Handled = true;
            this.OnTouchDrag(e.DeltaManipulation.Translation.X);
        }

        void AppHelpControl_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
        {
            e.Handled = true;
            FrameworkElement originalSource = e.OriginalSource as FrameworkElement;
            Point point = e.ManipulationContainer.TransformToVisual(this).Transform(e.ManipulationOrigin);
            double x = e.TotalManipulation.Translation.X;
            if (this.pageIndex <= 0 && x > 0)
            {
                return;
            }
            this.OnTouchFlick(originalSource, point, e.TotalManipulation.Translation.X);
        }

        private void OnTouchDrag(double xDelta)
        {
            if (this.pageIndex <= 0 && xDelta >= 0 || this.pageIndex >= 4 && xDelta <= 0)
            {
                return;
            }
            TranslateTransform renderTransform = this.OneCanvas.RenderTransform as TranslateTransform;
            renderTransform.X = renderTransform.X + xDelta;
            TranslateTransform x = this.TwoCanvas.RenderTransform as TranslateTransform;
            x.X = x.X + xDelta;
            TranslateTransform translateTransform = this.ThreeCanvas.RenderTransform as TranslateTransform;
            translateTransform.X = translateTransform.X + xDelta;
        }

        private void OnTouchFlick(FrameworkElement touchTarget, Point position, double xTotal)
        {
            double num = xTotal;
            if (num < 0)
            {
                if (this.pageIndex >= 4)
                {
                    return;
                }
                AppHelpControl appHelpControl = this;
                appHelpControl.pageIndex = appHelpControl.pageIndex + 1;
            }
            else if (num <= 0)
            {
                if (this.pageIndex >= 4)
                {
                    return;
                }
                AppHelpControl appHelpControl = this;
                appHelpControl.pageIndex = appHelpControl.pageIndex + 1;
            }
            else
            {
                if (this.pageIndex <= 0)
                {
                    return;
                }
                AppHelpControl appHelpControl = this;
                appHelpControl.pageIndex = appHelpControl.pageIndex - 1;
            }
            this.MovePage(((TranslateTransform)this.OneCanvas.RenderTransform).X, -(this.LayoutRoot.ActualWidth * (double)this.pageIndex));
        }

        private void MovePage(double from, double to)
        {
            if (this.pageTurnStoryboard.GetCurrentState() != ClockState.Active)
            {
                int actualWidth = (int)this.LayoutRoot.ActualWidth;
                this.animation1.From = new double?(from);
                this.animation1.To = new double?(to);
                this.animation2.From = new double?(from + (double)actualWidth);
                this.animation2.To = new double?(to + (double)actualWidth);
                this.animation3.From = new double?(from + (double)(actualWidth * 2));
                this.animation3.To = new double?(to + (double)(actualWidth * 2));
                this.animation4.From = new double?(from + (double)(actualWidth * 3));
                this.animation4.To = new double?(to + (double)(actualWidth * 3));
                this.pageTurnStoryboard.Completed += pageTurnStoryboard_Completed;
                this.pageTurnStoryboard.Begin();
            }
        }

        void pageTurnStoryboard_Completed(object sender, EventArgs e)
        {

            this.pageTurnStoryboard.Completed -= this.pageTurnStoryboard_Completed;
            if (this.pageIndex == 4)
            {
                base.Visibility = Visibility.Collapsed;                
                if (this._BasePageName == ComicIslandPage.StartPage)
                {
                    //IndexPage.xaml为启动页时
                    this._BasePage.ApplicationBar.IsVisible = true;
                    (this._BasePage as IndexPage).RemoveHelp();                    
                }                
                else if(this._BasePageName == ComicIslandPage.PersonalCenterPage)
                {
                    //帮助页查看帮助信息时
                    this._BasePage.ApplicationBar.IsVisible = true;
                    (this._BasePage as PersonalCenterPage).RemoveHelp();
                }
                
            }
        }




    }
}